<template>
	<view class="conterScroll">
		<view class="searchPosi">
			<view class="searchInput"><u-search shape="square" :clearabled="true" :show-action="false"
				v-model="title" placeholder='请输入家居名称' @search="updateParams" ></u-search></view>
		</view>
		<!-- 没有数据且未加载时的提示 -->
		<view class="nullData" v-if="arrList.length == 0 && !isLoading" >
			<image src="https://img.zhizhuangtong.com/project_media/img/xcx/null.png" mode=""></image>
			<text>暂无数据</text>
		</view>
		<view class="list" v-else>
			<view class="box">
				<view class="flex_row_btw_center">
					<view class="title">
						最近搜索
					</view>
					<view class="del" @tap="delItem('0','')">
						<up-icon name="trash" color="#999" size="15"></up-icon>
					</view>
				</view>
			</view>
			<view class="u-flex-wrap-reverse">
				<view v-for="(item,index) in arrList" :key="index" class="item" >
					<view class="flex_row_btw_center itemInfo">
						<text @tap="searchItem(item.search)">{{item.search}}</text>
						<up-icon name="close" color="#999" size="12" @tap="delItem(item.id,index)"></up-icon>
					</view>
				</view>
			</view>
		</view>
	</view>
	<view v-if="isLoading" class="load-more-box">
		<text>加载中...</text>
	</view>
</template>

<script setup>
	import {
		onLoad,
		onShow,
		onReachBottom,
		onPullDownRefresh,
		onShareAppMessage,
		onShareTimeline,
		onHide,
		onNavigationBarButtonTap,
		onBackPress
	} from '@dcloudio/uni-app';
	import {
		ref,
		onMounted
	} from 'vue';
	import {
		listDataHistory,
		delHistory
	} from '@/api/homeFurnishings.js';
	const isLoading = ref(false)
	// onLoad((option) => {
	// 	wx.showShareMenu({
	// 		withShareTicket: true,
	// 		menus: ["shareAppMessage", "shareTimeline"]
	// 	})

	// })
   
    onNavigationBarButtonTap((e)=>{
		console.log(e)
	})
	
    const title = ref('')
	//发送给朋友
	onShareAppMessage((res) => {
		console.log('发送给朋友发送给朋友', res)

		if (casedetail.value) {
			const url = '/pages/home/index?share=1' //你的转发页面路径拼接参数
			console.log('url', url)
			return {
				title: '中鼎智装通',
				type: 0,
				path: url,
				// summary: casedetail.value.desc,
				// imageUrl: casedetail.value.img
			}
		}
	})

	//分享到朋友圈
	onShareTimeline((res) => {
		console.log('分享到朋友圈')
		if (casedetail.value) {
			const url = '/pages/home/index?share=1' //你的转发页面路径拼接参数
			console.log('url', url)
			return {
				title: '中鼎智装通',
				type: 0,
				path: url,
				query: {
					id: id.value,
					// share: '1' // 确保 share=1 存在
				},
				// summary: casedetail.value.desc,
				// imageUrl: casedetail.value.img
			}
		}
	})


	onPullDownRefresh(() => {
		console.log('下拉刷新触发')
		wx.showLoading({
			title: '加载中',
			mask: true, // 是否显示透明蒙层，防止触摸穿透，默认false
			duration: 1000, // 显示时间，单位毫秒，默认1500

		});

		setTimeout(() => {

			wx.hideLoading();
			uni.stopPullDownRefresh();
		}, 1000);
	})

	onShow(() => {
		queryList()
		// 获取当前页面栈
		const pages = getCurrentPages();
		// 获取当前页面的索引（注意：索引是从0开始的，即第一个页面的索引是0）
		const currentPageIndex = pages.length - 1;
		console.log(pages)
		console.log(currentPageIndex,222)
	})
	onBackPress((options)=> {
			console.log('from:' + options)
		})
	const arrList = ref([])
	// 请求数据列表
	const queryList = async () => {
		isLoading.value = true; // 设置加载中
		try {
			const res = await listDataHistory();
			if (res.code === 1) {
				arrList.value = res.data.list
			}
			setTimeout(() => {
				isLoading.value = false;
			}, 500);
		} catch (error) {
			console.error('请求数据失败:', error);
		} finally {
			isLoading.value = false; // 加载完成
		}
	};
	//查询
	const updateParams = () => {
		if(title.value!=''){
			uni.navigateTo({
				url: '/pages/find/homeFurnishSearch/index?title='+title.value+'&catId=' + '99999'
			});
			title.value = ''
		}
	}
	
	//删除搜索历史记录
	const delItem = async(code,index)=>{
		const res = await delHistory({id:code})
		wx.showToast({
			title: res.msg,
			icon: 'none',
			duration: 2000
		});
		if(code==0){
			if(res.code==1){
				arrList.value = []
			}
			
		}else{
			if(res.code==1){
				arrList.value.splice(index,1)
			}
		}
		
	}
	const searchItem = (code)=>{
		uni.navigateTo({
			url: '/pages/find/homeFurnishSearch/index?title='+code+'&catId=' + '99999'
		});
	}
</script>


<style lang="less" scoped>
	.searchPosi {
		position: fixed;
		z-index: 999999;
		background-color: #fff;
		width: 100%;
		justify-content: center;
		height: 118rpx;
		display: flex;
		align-items: center;
        top: 0;
		.searchInput {
			width: calc(100% - 60rpx);
		}
	}

	.conterScroll {
		width: 100%;
		height: 100vh;
		// overflow-y: auto;
		background: #f3f3f3;
		.nullData {
			height: calc(100% - 110rpx);
		}

		.list {
			margin: 148rpx 30rpx 0;
			.box {
				margin-bottom:  20rpx;
				.title {
					font-weight: 400;
					font-size: 30rpx;
					color: #333333;
				}
			}
            .item{
				display: inline-block;
				.itemInfo{
					font-weight: 400;
					font-size: 24rpx;
					padding: 6rpx 15rpx;
					color: #333333;
					border-radius: 10rpx 10rpx 10rpx 10rpx;
					border: 1rpx solid #B9B9B9;
					margin-right: 25rpx;
					margin-bottom: 20rpx;
					text{
						margin-right: 20rpx;
					}
				}
			}
		}
	}

	.load-more-box {
		text-align: center;
		width: 100%;
		padding-top: 20rpx;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
		color: #7A7A7A;
	}
</style>